package cn.xutongxue.doc.utils;

import cn.xutongxue.doc.config.CosClientConfig;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * 腾讯云对象存储工具类
 */
@Component
@RequiredArgsConstructor
public class CosUtil {

    private final CosClientConfig cosClientConfig;
    private final COSClient cosClient;

    /**
     * 上传对象
     * @param key
     * @param file
     * @return
     */
    public PutObjectResult putObject(String key, File file) {
        return cosClient.putObject(new PutObjectRequest(cosClientConfig.getBucket(), key, file));
    }

    /***
     * 下载对象
     * @param key
     * @return
     */
    public COSObject getObject(String key) {
        return cosClient.getObject(new GetObjectRequest(cosClientConfig.getBucket(), key));
    }

}
